
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.document.operation.impl {
import org.goverla.collections.ArrayList;

import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.AttributesUpdate;
import org.waveprotocol.wave.model.document.operation.BufferedDocOp;
import org.waveprotocol.wave.model.document.operation.EvaluatingDocOpCursor;

/**
 * This buffers the operation and returns it as a {@link BufferedDocOp}.
 *
 * The operation returned is not guaranteed to be well-formed, as there is no
 * check upon completion by default.
 *
 * Use sparingly. Its use should only be for ill-formedness tests, and possibly
 * for efficiency if profiling reveals it to be necessary. If in doubt, use
 * DocOpBuffer which does a well-formedness check.
 */
public class UncheckedDocOpBuffer implements EvaluatingDocOpCursor {
	
	private static var EMPTY_ARRAY:Array = new Array();
	private var accu:ArrayList= new ArrayList();
	
	public function UncheckedDocOpBuffer() {}
	//Behaviour is undefined if this buffer is used after calling this method.
	public function finish():BufferedDocOp {
		return finishUnchecked();
	}
	
	/**
	 * Finish and do a well formedness check as well
	 *
	 * Behaviour is undefined if this buffer is used after calling this method.
	 */
	public final function finishChecked():BufferedDocOp {
		return BufferedDocOpImpl.create(accu);
	}
	
	public final function finishUnchecked():BufferedDocOp {
		return BufferedDocOpImpl.createUnchecked(accu);
	}
	public final function annotationBoundary(map:AnnotationBoundaryMap):void {
		accu.addItem(new AnnotationBoundary(map));
	}
	
	public final function characters(s:String):void {
		accu.addItem(new Characters(s));
	}
	
	public final function elementEnd():void {
		accu.addItem(ElementEnd.INSTANCE);
	}
	
	public final function elementStart(type:String, attrs:Attributes):void {
		accu.addItem(new ElementStart(type, attrs));
	}
	
	public final function deleteCharacters(s:String):void {
		accu.addItem(new DeleteCharacters(s));
	}
	
	public final function retain(itemCount:int):void {
		accu.addItem(new Retain(itemCount));
	}
	
	public final function deleteElementEnd():void {
		accu.addItem(DeleteElementEnd.INSTANCE);
	}
	
	public final function deleteElementStart(type:String, attrs:Attributes):void {
		accu.addItem(new DeleteElementStart(type, attrs));
	}
	
	public final function replaceAttributes(oldAttrs:Attributes, newAttrs:Attributes):void {
		accu.addItem(new ReplaceAttributes(oldAttrs, newAttrs));
	}
	
	public final function updateAttributes(update:AttributesUpdate):void {
		accu.addItem(new UpdateAttributes(update));
	}
}
}